from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
import pandas as pd
import datetime

df = pd.read_excel('C:/Users/zhang/Desktop/stock.xls',index_col = 'date' ,date_parser='date')
trainDF_Ascending = df.sort_index(ascending=True) #日期升序
trainDF_Descending = df.sort_index(ascending=False) #日期降序

def date_range(start_date,end_date):
    for n in range(int((end_date-start_date).days)):
        yield start_date+datetime.timedelta(n)



start=datetime.datetime(2021,1,1,0,0,0)
end=datetime.datetime(2021,12,31,0,0,0)

for day in date_range(start, end):
    print(day.strftime('%Y-%m-%d'))
    ppt=trainDF_Ascending[day:day].groupby('code').sum().sort_values(by='rate').tail(10)
    print(ppt)
    # for i in pd.date_range('20210101', '20211230',freq='YS')
    for row in ppt.itertuples():
        print(row.Index,getattr(row, 'rate'), getattr(row, 'price')) # 输出每一行
        
    bar = (
        Bar()
        .add_xaxis(pd.Series(ppt.index, index=ppt.index).values())
        .add_yaxis("商家A", pd.Series(ppt['rate'], index=ppt.index))
        .set_global_opts(
            title_opts=opts.TitleOpts("某商店{}年营业额 - With Graphic 组件".format(i)),
            graphic_opts=[
                opts.GraphicGroup(
                    graphic_item=opts.GraphicItem(
                        rotation=JsCode("Math.PI / 4"),
                        bounding="raw",
                        right=100,
                        bottom=110,
                        z=100,
                    ),
                    children=[
                        opts.GraphicRect(
                            graphic_item=opts.GraphicItem(
                                left="center", top="center", z=100
                            ),
                            graphic_shape_opts=opts.GraphicShapeOpts(
                                width=400, height=50
                            ),
                            graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                fill="rgba(0,0,0,0.3)"
                            ),
                        ),
                        opts.GraphicText(
                            graphic_item=opts.GraphicItem(
                                left="center", top="center", z=100
                            ),
                            graphic_textstyle_opts=opts.GraphicTextStyleOpts(
                                text="某商店{}年营业额".format(i),
                                font="bold 26px Microsoft YaHei",
                                graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                    fill="#fff"
                                ),
                            ),
                        ),
                    ],
                )
            ],
        )
    )
    tl.add(bar, "{}年".format(i))
tl.render("timeline_bar_with_graphic.html")
